//#########################################################
//#
//# Titre : 	Utilitaires Liste Chainee et CHAT LINUX Automne 13
//#			SIF-1015 - Systeme d'exploitation
//#			Universite du Quebec a Trois-Rivieres
//#
//# Auteur : 	Nathan Lachance,
//#             James Hoffman
//#	Date :	Septembre 2013
//#
//# Langage : 	ANSI C on LINUX 
//#
//#######################################

#include "c.h"

int main()

{

    int server_fifo_fd, client_fifo_fd;

    struct Info_FIFO_Transaction my_data;

    int times_to_send;

    char client_fifo[256];



    server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);

    if (server_fifo_fd == -1) {

        fprintf(stderr, "Sorry, no server\n");

        exit(EXIT_FAILURE);

    }

    my_data.pid_client = getpid();

    //sprintf(client_fifo, CLIENT_FIFO_NAME, my_data.pid_client);
createFifoClient(my_data.pid_client, client_fifo);


// For each of the five loops, the client data is sent to the server.

// Then the client FIFO is opened (read-only, blocking mode) and the data read back.

// Finally, the server FIFO is closed and the client FIFO removed from memory.


        sprintf(my_data.transaction, "A lol ici"); 
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");


	server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");

	/*server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");
	server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");
	server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");
*/

        sprintf(my_data.transaction, "T roger ici blblblblblblblbla"); 

	server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");

 	server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
        write(server_fifo_fd, &my_data, sizeof(my_data));
	close(server_fifo_fd);
	printf("--data sent\n");


int i;
char message[MESSAGE_SIZE];
struct Server_Response sr;
client_fifo_fd = open(client_fifo, O_RDONLY);
	for(i = 0;i<4;i++)
	{
		printf("A %i\n", i);
		
		memset(message, '\0', sizeof(message));

		if (client_fifo_fd != -1) {

			int count = FALSE;
			
			printf("B\n");

			read(client_fifo_fd, &sr, sizeof(sr));
			printf("1er read %i - %s\n",sr.lastMessage, sr.message);
			int p = 0;
			printf("C\n");
			while(sr.lastMessage == TRUE)
			{printf("while %i\n", ++p);
				read(client_fifo_fd, &sr, sizeof(sr));
				printf("2eme read %i - %s\n",sr.lastMessage, sr.message);
			}
			
		}
		else
		{
		printf("Fail\n");
		}

		
	}
close(client_fifo_fd);

    close(server_fifo_fd);

    unlink(client_fifo);

    exit(EXIT_SUCCESS);

}

int createFifoClient(unsigned int pid, char clientFifoName[FIFO_NAME_SIZE])
{
	memset(clientFifoName, '\0', sizeof(clientFifoName));
	sprintf(clientFifoName, "../FIFOs/FIFO_%d", pid);

	if(access(clientFifoName, F_OK) == -1)
	{
		Mkfifo(clientFifoName, 0777);

		return 0;//n'existait pas
	}

return 1;//Existe deja
}

void Mkfifo(const char *path, mode_t mode)
{
	if(mkfifo(path, mode) != 0)
	{

		printf("\n\n>>> Erreur a la creation du FIFO!\n\n");
		
		exit(EXIT_FAILURE);
	}
}




